import 'dart:collection';

import 'package:flutter/material.dart';

import 'words_highlight.dart';

class TextHighlight extends StatelessWidget {
  final TextStyle normalStyle; //正常样式
  final TextStyle highlightStyle; //高亮样式
  final String content; //字符串
  final List<String> keyWords; //字符串中需要高亮的关键词组
  final int maxLines; // 最大行数

  TextHighlight(
      {this.content,
      this.normalStyle,
      this.highlightStyle,
      this.maxLines,
      this.keyWords});

  @override
  Widget build(BuildContext context) {
    if (this.keyWords == null || this.keyWords.isEmpty) {
      return Text(
        content,
        style: normalStyle,
        maxLines: maxLines,
        overflow: TextOverflow.ellipsis,
      );
    } else {
      Map<String, HighlightedWord> words = {};
      for (String w in keyWords) {
        words[w] = HighlightedWord(
          onTap: () {},
          textStyle: highlightStyle,
        );
      }

      return WordHighlight(
        text: content,
        words: words as LinkedHashMap<String, HighlightedWord>,
        textStyle: normalStyle,
        maxLines: maxLines,
        textAlign: TextAlign.justify,
        overflow: TextOverflow.ellipsis,
        enableCaseSensitive: true,
      );
    }
  }
}
